package system.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import system.Convert;

/***
 * DataRow 由一个DataTable 创建而来，
 * DataRow 实际是一个列表，DataRow 从 DataTable 中获得列名和索引的对应关系并实现Map 接口实现按列取值
 * 这个结构不应该单独存在只作为DataTable 的子项
 * @author Fred
 */
public class DataRow implements Map
{
    private ArrayList fields;
    private DataTable parentTable;
    private int column_count;

    /***
     *  这个方法不应该被程序员调用
     * @param dTable
     */
    public DataRow(DataTable dTable)
    {
        this.fields = new ArrayList();
        this.parentTable = dTable;
        this.column_count=0;
    }

    /***
     *  将另外一张表的一行的列数据补充到当前行的已有列中
     * @param dr
     * @return 当前行（已经补充完数据的）
     */
    public DataRow join(DataRow dr)
    {
        if(dr==null)
            return this;
        Set<String> key_set = dr.keySet();
        //dr 的所有key 覆盖到当前行的所有同名key中
        for(String k : key_set)
            this.put(k, dr.get(k));
        return this;
    }

    /***
     * 给 ArrayList 表示的行数据按照列索引设置值
     * 注意如果当前列索引超出了 ArrayList  最后一个元素的索引下标，则将缺失的所有元素用空补上。
     * @param key 列名
     * @param value 列值
     * @return 设置前列的旧值
     */
    public Object put(Object key, Object value)
    {
        Integer column_index = this.parentTable.column_index_map.get(key);
        if(column_index==null)
        	return null;
        
        int j = column_index.intValue();
        while(this.column_count<j+1)
        {
        	this.fields.add(null);
        	this.column_count++;
        }

        this.fields.set(j,value);
        return null;
    }

    public Object get(Object key)
    {
        Integer column_index = this.parentTable.column_index_map.get(key);
        if(column_index==null)
        	return null;
         int j = column_index.intValue();

         if(j<this.column_count)
            return this.fields.get(j);
         return null;
    }

    //<editor-fold defaultstate="collapsed" desc="only_for_implements_Map">
    public String getString(Object key)
    {
        return Convert.toString(get(key));
    }

    public int getInt(Object key)
    {
        return Convert.toInt(get(key));
    }

    public float getFloat(Object key)
    {
        return Convert.toFloat(get(key));
    }

    public Date getDate(Object key)
    {
        return Convert.toDate(get(key));
    }

    public void clear()
    {
        this.fields.clear();
    }

    public boolean containsKey(Object key)
    {
        return this.parentTable.column_index_map.containsKey(key);
    }

    public boolean containsValue(Object value)
    {
        return this.parentTable.column_index_map.containsValue(value);
    }

    public Set entrySet()
    {
        return this.parentTable.column_index_map.entrySet();
    }

    public boolean isEmpty()
    {
        return this.fields.isEmpty();
    }

    public void putAll(Map m)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object remove(Object key)
    {
        Integer column_index = this.parentTable.column_index_map.get(key);
        if(column_index==null)
        	return null;

         int j = column_index.intValue();
         return this.fields.remove(j);
    }

    public int size()
    {
        return this.parentTable.column_count;
    }

    public Set keySet()
    {
        return this.parentTable.column_index_map.keySet();
    }
    
    public Collection values()
    {
         return this.parentTable.column_index_map.values();
    }
    //</editor-fold>
}
